﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace DataProject
{
    public static class DBController
    {
        private static readonly DataProject.LibraryModelContainer EntityStorage;

        static DBController()
        {
            EntityStorage = new DataProject.LibraryModelContainer();
        }

        public static void CreateBook(Book book)
        {
            if (book == null) throw new ArgumentNullException("DataProject.DBController CreateBook method Book entity was null");

            EntityStorage.AddToBookSet(book);
            EntityStorage.SaveChanges();
        }

        public static int BookCount()
        {
            return EntityStorage.BookSet.Count();
        }

        public static Book GetBookById(int Id)
        {
            return EntityStorage.BookSet.First(m => m.Id == Id);
        }

        public static List<Book> GetListOfBook()
        {
            return EntityStorage.BookSet.ToList();
        }
        public static List<Book> GetListOfBook(int ItemsPerPage, int PageNumber, bool SortByBookTitle)
        {
            if (PageNumber < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            List<Book> list = EntityStorage.BookSet.ToList();
            list =
                SortByBookTitle ?
                list.OrderBy(m => m.Title).ToList() :
                list.OrderBy(m => m.Author.First().FName).ToList();

            List<Book> returnList = new List<Book>();
            for (int i = PageNumber * ItemsPerPage; (i < PageNumber * ItemsPerPage + ItemsPerPage) && i < list.Count; i++)
            {
                returnList.Add(list[i]);
            }
            return returnList;
        }

        public static void DeleteBook(int id)
        {
            EntityStorage.DeleteObject(GetBookById(id));
            EntityStorage.SaveChanges();
        }

        public static void UpdateBook(Book book)
        {
            if (GetBookById(book.Id) == null)
            {
                throw new ArgumentException(String.Format("The Book with Id {0} not exists is Database, so it cannot be updeted", book.Id));
            }
            Book entityBook = new Book();
            entityBook.Id = book.Id;
            entityBook.Description = book.Description;
            entityBook.Title = book.Title;
            entityBook.Year = book.Year;

            for (int i = 0; i < book.Author.Count; i++)
            {
                Author temp = book.Author.ToList()[i]; // this is new element, and we need the old authorId by Book.Id
                int autorId = 0;
                autorId = GetBookById(book.Id).Author.ToList()[i].Id;
                Author author = new Author { Id = autorId, FName = temp.FName, LName = temp.LName, EntityKey = temp.EntityKey };
                entityBook.Author.Add(author);
                EntityStorage.ApplyCurrentValues("AuthorSet", author);
            }
            for (int i = 0; i < book.Client.Count; i++)
            {
                Client temp = book.Client.ElementAt(i); // this is new element, and we need the old clientId by Book.Id
                int clientId = GetBookById(book.Id).Client.ToList()[i].Id;
                Client client = new Client
                {
                    Id = clientId,
                    FName = temp.FName,
                    LName = temp.LName,
                    EntityKey = temp.EntityKey,
                    Adress = temp.Adress,
                    Email = temp.Email,
                    Phone = temp.Phone
                };

                entityBook.Client.Add(client);
                EntityStorage.ApplyCurrentValues("ClientSet", client);
            }
            for (int i = 0; i < book.Period.Count; i++)
            {
                Period temp = book.Period.ElementAt(i); // this is new element, and we need the old periodId by Book.Id
                Period oldPeriod = GetBookById(book.Id).Period.ToList()[i];
                Period period = new Period
                {
                    Id = oldPeriod.Id,
                    BeginDate = temp.BeginDate,
                    EndDate = temp.EndDate,
                };

                entityBook.Period.Add(period);
                EntityStorage.ApplyCurrentValues("PeriodSet", period);
            }

            EntityStorage.ApplyCurrentValues("BookSet", entityBook);
            EntityStorage.SaveChanges();
        }
    }
}
